const startFn = () => {
    initChangeBtn();
    initFilterBtn();
    initAddBtn();
    initOpenOrCloseBtn();
    initEditBox();
    initEditSubmitBtn();
    initSyncBtn();
    initErrorTip();
    window.electronAPI.filterFile({
        // 过滤字段
        filterStr: ''
    })
    readFile();
};
const watchingFn = () => {
    watchingMain();// 获取弹窗
}
// 初始化功能按钮
function initChangeBtn() {
    document.getElementById("setSmallSizeSmall").addEventListener("click", () => {
        window.electronAPI.setSize({ width: 80, height: 25 });
    })
    // document.getElementById("setSmallSizeCenter").addEventListener("click",()=>{
    //     window.electronAPI.setSize({width:300,height:25});
    // })
    document.getElementById("setSmallSizeBig1").addEventListener("click", () => {
        window.electronAPI.setSize({ width: 300, height: 400 });
    })
    document.getElementById("setSmallSizeBig").addEventListener("click", () => {
        window.electronAPI.setSize({ width: 300, height: 400 });
    })
    document.getElementById("syncData").addEventListener("click", () => {
        readFile();
    })
}
/**
 * 初始化过滤按钮和清空过滤按钮
 */
function initFilterBtn() {
    window.electronAPI.readFile()
    const filterInput = document.getElementsByName("filterInput")[0];
    const filterBtn = document.getElementsByName("filterBtn")[0];
    const clearFilterBtn = document.getElementsByName("clearFilterBtn")[0];
    filterBtn.addEventListener("click", () => {
        // 过滤方法
        window.electronAPI.filterFile({
            // 过滤字段
            filterStr: filterInput.value
        })
    })

    clearFilterBtn.addEventListener("click", () => {
        filterInput.value = "";
        // 过滤方法
        window.electronAPI.filterFile({
            // 过滤字段
            filterStr: ""
        })
    })
}
// 初始化添加模块按钮
function initAddBtn() {
    const nameInput = document.getElementsByName("nameInput")[0];
    const linkInput = document.getElementsByName("linkInput")[0];
    const dateInput = document.getElementsByName("dateInput")[0];
    const addBtn = document.getElementsByName("addBtn")[0];
    console.log(nameInput, linkInput, dateInput, addBtn)
    addBtn.addEventListener("click", async () => {
        if (!nameInput.value) return;
        if (!linkInput.value) return;
        if (!dateInput.value) return;
        // 添加方法
        window.electronAPI.setupFile({
            type: "add",
            id: String(Date.parse(new Date())),
            nameValue: nameInput.value,
            linkValue: linkInput.value,
            startDate: dateInput.value && dateInput.value.replaceAll("-", "/")
        })
        clearAddData()
    })
}
// 初始化错误提示弹框事件
function initErrorTip() {
    const errorTip = document.getElementById("errorLoading");
    errorTip.addEventListener("click", () => {
        errorTip.style.display = "none";
    })
}
/**
 * @param {string} id id表示
 */
function completeFn(id) {
    window.electronAPI.setupFile({
        type: "complete",
        id
    })
}
// 添加隐藏切换
const OpenOrCloseFn = (() => {
    let showSign = false;
    return () => {
        console.log(showSign)
        showSign = !showSign;
        return showSign;
    }
})()
// 添加模块显示隐藏隐藏按钮初始化
function initOpenOrCloseBtn() {
    const openOrClose = document.getElementsByName("openOrClose")[0];
    const addBoxDom = document.getElementsByName("addBox")[0];
    openOrClose.addEventListener("click", () => {
        if (OpenOrCloseFn()) {
            // 不显示
            addBoxDom.style.display = "block";
        } else {
            // 显示
            addBoxDom.style.display = "none";
        }
    })
}
// 监听主进程返回数据
function watchingMain() {
    /**
     * 监听主进程
     * @param {Array} data 返回表格数据
     */
    window.electronAPI.returnFileData()
    window.electronAPI.closeLoading()
    window.electronAPI.errorLoading()
    window.electronAPI.watchPublicInfoUpdate()
    window.electronAPI.watchPublicInfoTip()
}
// 监听主进程传递信息
function readFile() {
    window.electronAPI.readFile()
}
// 初始化编辑盒子
function initEditBox() {
    // 获取 <span> 元素，用于关闭弹窗
    const span = document.querySelector('.close');
    // 点击 <span> (x), 关闭弹窗
    span.onclick = function () {
        openEditDialog({ type: 2 })
    }
    // 在用户点击其他地方时，关闭弹窗
    // window.onclick = function (event) {
    //     if (event.target == modal) {
    //         openEditDialog({ type: 2 })
    //     }
    // }
}
/**
 * 初始化编辑提交
 */
function initEditSubmitBtn() {
    const editNameInput = document.getElementsByName("editNameInput")[0];
    const editLinkInput = document.getElementsByName("editLinkInput")[0];
    const editDateInput = document.getElementsByName("editDateInput")[0];
    const editBtn = document.getElementsByName("editBtn")[0];
    editBtn.addEventListener("click", () => {
        // 添加方法
        window.electronAPI.setupFile({
            type: "edit",
            id: editBtn.getAttribute("id"),
            nameValue: editNameInput.value,
            linkValue: editLinkInput.value,
            startDate: editDateInput.value && editDateInput.value.replaceAll("-", "/")
        })
        clearEditData()
        openEditDialog({ type: 2 })
    })
}
/**
 * 初始化同步禅道按钮
 */
function initSyncBtn() {
    const updateDetailsBtn = document.getElementById("updateDetails");
    const selectMode = document.getElementById("syncDataMode");
    const syncBtn = document.getElementById("syncBtn");
    const watchBtn = document.getElementById("watchBtn");
    const favDialog = document.getElementById("favDialog");
    updateDetailsBtn.addEventListener("click", () => {
        favDialog.showModal();
    })
    // 同步
    syncBtn.addEventListener("click", () => {
        const syncDataZentaosid = document.getElementById("syncDataZentaosid").value;
        console.log(syncDataZentaosid)
        if (!syncDataZentaosid) {
            return
        }
        document.getElementById("loadingBox").classList.add("isLoading");
        window.electronAPI.getChanDaoData({
            mode: selectMode.value,
            zentaosid: syncDataZentaosid
        })
        selectMode.value = "1";
    })
    // 订阅
    watchBtn.addEventListener("click", () => {
        const watchPublishSystemId = document.getElementById("watchPublishSystemId").value;
        const packageId = document.getElementById("packageId").value;
        if (!watchPublishSystemId) {
            return
        }
        if(!packageId){
            return
        }
        // document.getElementById("loadingBox").classList.add("isLoading");
        window.electronAPI.watchPublicInfo({
            mode: selectMode.value,
            publish_system_sess_: watchPublishSystemId,
            packageId
        })
        selectMode.value = "1";
    })
}


/**
 * 编辑方法
 * @param {string} arrstr 数据字符
 */
function editFn(arrstr) {
    const rowData = arrstr.split(",");
    const editNameInput = document.getElementsByName("editNameInput")[0];
    const editLinkInput = document.getElementsByName("editLinkInput")[0];
    const editDateInput = document.getElementsByName("editDateInput")[0];
    const editBtn = document.getElementsByName("editBtn")[0];
    editNameInput.value = rowData[0];
    editLinkInput.value = rowData[1];
    editDateInput.value = (rowData[2]).replaceAll("/", "-");
    editBtn.setAttribute("id", rowData[4])
    openEditDialog({ type: 1 })
}
/**
 * 优先处理
 */
function upHierarchicalStructureFn(id){
    window.electronAPI.setupFile({
        type: "upHierarchicalStructure",
        id: id
    })
}
/**
 * 删除按钮
 * @param {string} delId 删除id
 */
function delFn(delId) {
    window.electronAPI.setupFile({
        type: "del",
        id: delId
    })
}
/**
 * 回退方法
 */
function resetFn(resetId) {
    window.electronAPI.setupFile({
        type: "reset",
        id: resetId
    })
}